<div class="task">
<?php include ('buttons.tpl'); ?>
</div>
<?php if ($error) { ?>
<div class="warning"><?php echo $error; ?></div>
<?php } ?>
<h2><?php echo $heading_title; ?></h2>
<div class="description"><?php echo $heading_description; ?></div>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
  <div id="tabs">
	<ul>
	  <li><a href="#tabs-1"><?php echo $tab_general; ?></a></li>
	</ul>
	<div id="tabs-1">
      <table>
        <tr>
		  <td><?php echo $entry_status; ?></td>
		  <td><select name="status">
		    <option value="1" <?php echo ($status)?' selected="selected"':'' ?>><?php echo $text_enabled; ?></option>
		    <option value="0" <?php echo (!$status)?' selected="selected"':'' ?>><?php echo $text_disabled; ?></option>
		  </select></td>
		</tr>
		<tr>
	      <td valign="top"><?php echo $entry_product; ?></td>
	      <td><select name="product[]" id="product" multiple="multiple" size="20" style="min-width: 300px;" onchange="loopSelected();"> 
	        <?php foreach ($products as $product) { ?>
	        <?php if ($product['special_id']) { ?>
	        <option value="<?php echo $product['product_id']; ?>" selected="selected"><?php echo $product['name']; ?> [<?php echo $product['regprice']; ?>]</option>
	        <?php } else { ?>
	        <option value="<?php echo $product['product_id']; ?>"><?php echo $product['name']; ?> [<?php echo $product['regprice']; ?>]</option>
	        <?php } ?>
	        <?php } ?>
	      </select></td>
	    </tr>
	    <tr>
          <td><?php echo $entry_prefix; ?></td>
          <td><select name="prefix" id="prefix" onchange="loopSelected();">
              <option value="%" <?php echo ($prefix == '%')?' selected="selected"':'' ?>><?php echo $text_percent; ?></option>
              <option value="-" <?php echo ($prefix == '-')?' selected="selected"':'' ?>><?php echo $text_minus; ?></option>
              <option value="=" <?php echo ($prefix == '=')?' selected="selected"':'' ?>><?php echo $text_equal; ?></option>
            </select></td>
        </tr>
        <tr>
          <td><span class="required">*</span> <?php echo $entry_discount; ?></td>
          <td><input type="text" id="discount" name="discount" onchange="loopSelected();" value="<?php echo $discount; ?>" />
            <?php if ($error_discount) { ?>
            <span class="error"><?php echo $error_discount; ?></span>
            <?php } ?>
          </td>
        </tr>
        <tr>
          <td><?php echo $entry_date_start; ?></td>
          <td><input name="date_start_day" value="<?php echo $date_start_day; ?>" size="2" maxlength="2" />
            <select name="date_start_month">
              <?php foreach ($months as $month) { ?>
              <?php if ($month['value'] == $date_start_month) { ?>
              <option value="<?php echo $month['value']; ?>" selected="selected"><?php echo $month['text']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $month['value']; ?>"><?php echo $month['text']; ?></option>
              <?php } ?>
              <?php } ?>
            </select>
            <input name="date_start_year" value="<?php echo $date_start_year; ?>" size="4" maxlength="4" />
            <?php if ($error_date_start) { ?>
            <span class="error"><?php echo $error_date_start; ?></span>
            <?php } ?></td>
        </tr>
        <tr>
          <td><?php echo $entry_date_end; ?></td>
          <td><input name="date_end_day" value="<?php echo $date_end_day; ?>" size="2" maxlength="2" />
            <select name="date_end_month">
              <?php foreach ($months as $month) { ?>
              <?php if ($month['value'] == $date_end_month) { ?>
              <option value="<?php echo $month['value']; ?>" selected="selected"><?php echo $month['text']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $month['value']; ?>"><?php echo $month['text']; ?></option>
              <?php } ?>
              <?php } ?>
            </select>
            <input name="date_end_year" value="<?php echo $date_end_year; ?>" size="4" maxlength="4" />
            <?php if ($error_date_end) { ?>
            <span class="error"><?php echo $error_date_end; ?></span>
          <?php } ?></td>
        </tr>
      </table>
    </div>
  </div>
  <script type="text/javascript"><!--
	$(function() {
		$("#tabs").tabs();
	});
  //--></script>
  <script type="text/javascript"><!--
  	//
	var origvals = new Array();
	var prodlist = document.getElementById('product');
	
	window.onload = loopSelected();
	
	function loopSelected()	{	  
	  var pfx = document.getElementById('prefix').value;
	  var disc = document.getElementById('discount').value;
	  var i;
	  var count = 0;
	  if (disc == '') { return;}
	  revertVal();
	  for (i=0; i<prodlist.options.length; i++) {
	  	origvals[i] = prodlist.options[i].text;
	    if (prodlist.options[i].selected) {
	      xstart = prodlist.options[i].text.indexOf("[");
	      xend = prodlist.options[i].text.indexOf("]");
	      price = prodlist.options[i].text.substring(xstart+1,xend);
	      price = price.replace(/[^0-9 /.]/g, ''); 
	      txt = prodlist.options[i].text
	      if (pfx == "%") {
	      	prodlist.options[i].text += ' => [' + eval(price - (price * (disc/100))).toFixed(2) + ']';
		  } else if (pfx == "-") {
		  	prodlist.options[i].text += ' => [' + eval(price - disc).toFixed(2) + ']';
		  } else {
		  	prodlist.options[i].text += ' => [' + eval(disc).toFixed(2) + ']';
		  }
	      count++;
	    }
	  }
	}
	
	function revertVal() {
	  var i;
	  var count = 0;
	  for (i=0; i<prodlist.options.length; i++) {
	    if (origvals[i] != undefined) {
	    	prodlist.options[i].text = origvals[i];
		}
	  }
	}
  //--></script>
</form>
